﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using QuanLyDiem.models;
using QuanLyDiem.Controllers;
namespace QuanLyDiem.DanhMuc
{
    public partial class frmSinhVien : DevExpress.XtraEditors.XtraForm
    {
        public frmSinhVien()
        {
            InitializeComponent();
        }

        private void frmSinhVien_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, false, false, false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            gridView1.DisplayFormatColumn(colNgay_sinh, "dd/MM/yyyy", DevExpress.Utils.FormatType.DateTime);
            C_GioiTinh.LoadDataSource(rleID_gioi_tinh, db);
            C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
            gridView1.BestFitAllColumns();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
            if (sv == null) return;
            txtdiachi.Text = sv.Dia_chi_tt;
            txtlop.Text = sv.Lop;
            txtmsv.Text = sv.Ma_sv;
            dteNgaysinh.EditValue = sv.Ngay_sinh;
            txtquequan.Text = sv.Que_quan;
            txtten.Text = sv.Ho_ten;
            rdNam.Checked = sv.ID_gioi_tinh == 0 ? true : false;
            rdNu.Checked = sv.ID_gioi_tinh == 1 ? true : false;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Enabled = btnxoa.Enabled = false;
            txtdiachi.Text = txtlop.Text = txtmsv.Text = txtquequan.Text = txtten.Text = "";
            dteNgaysinh.EditValue = new DateTime(1980, 01, 01);
            rdNam.Checked = true;
            txtmsv.Focus();
        }

        private void btnthuchien_Click(object sender, EventArgs e)
        {
            try
            {
                var sv = new HoSoSinhVien() 
                {
                    Dia_chi_tt=txtdiachi.Text,
                    Ho_ten=txtten.Text,
                    Ma_sv=txtmsv.Text,
                    Que_quan=txtquequan.Text,
                    Lop=txtlop.Text,
                    ID_gioi_tinh = rdNam.Checked == true ? 0 : 1,
                    Ngay_sinh = (DateTime)dteNgaysinh.EditValue

                };
                if (!C_SinhVien.Insert(sv)) throw new Exception();
                C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                gridView1.BestFitAllColumns();
                btnsua.Enabled = btnxoa.Enabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
                sv.Ho_ten = txtten.Text;
                sv.Dia_chi_tt = txtdiachi.Text;
                sv.Lop = txtlop.Text;
                sv.Ma_sv = txtmsv.Text;
                sv.Que_quan = txtquequan.Text;
                sv.ID_gioi_tinh = rdNam.Checked == true ? 0 : 1;
                sv.Ngay_sinh = (DateTime)dteNgaysinh.EditValue;
                if (!C_SinhVien.Update(sv)) throw new Exception();
                C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            try
            {
                var sv = gridView1.GetFocusedRow() as HoSoSinhVien;
                if (!C_SinhVien.Delete(sv)) throw new Exception();
                C_SinhVien.LoadViewBindingSource(hoSoSinhVienBindingSource);
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }
    }
}